package com.itheima.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.itheima.anno.Log;
import com.itheima.doman.Result;
import com.itheima.doman.pojo.PageResult;
import com.itheima.doman.pojo.StdQueryParam;
import com.itheima.doman.pojo.Student;
import com.itheima.execl.StudentListener;
import com.itheima.execl.model.StudentModel;
import com.itheima.mapper.StdMapper;
import com.itheima.service.StdService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StdController {
    @Autowired
    private StdService stdService;

    /**
     *  分页查询学生信息
     * @param stdQueryParam
     * @return
     */
    @GetMapping
    public Result page(StdQueryParam stdQueryParam){
        PageResult<Student> pageResult= stdService.page(stdQueryParam);
        return Result.success(pageResult);
    }

    /**
     *  批量删除学生信息
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        stdService.deleteByIds(ids);
        return Result.success();
    }

    /**
     *  添加学生信息
     * @param student
     * @return
     */
    @Log
    @PostMapping
    public Result addStd(@RequestBody Student student){
        stdService.addStd(student);
        return Result.success();
    }

    /**
     *  回显学生信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Student student=stdService.findById(id);
        return Result.success(student);
    }

    /**
     *  修改学生信息
     * @param student
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Student student){
        stdService.update(student);
        return Result.success();
    }




    /**
     *  添加学生违纪
     * @param id
     * @param score
     * @return
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id,@PathVariable Integer score){
           stdService.updateViolation(id, score);
           return Result.success();
    }
    /**
     * 导入学生信息
     */
    @PostMapping("/import/{clazzId}")
    public Result importStudent(MultipartFile file, @PathVariable Integer clazzId) throws IOException {
        log.info("批量导入学生信息");
        EasyExcel.read(file.getInputStream(),
                        StudentModel.class,
                        new StudentListener(stdService, clazzId))
                .sheet().doRead();
        return Result.success();
    }
    /**
     * 导出学生信息
     */
    @GetMapping("/export")
    public void exportStudent(HttpServletResponse response, String name, Integer degree, Integer clazzId) throws IOException {
        log.info("导出学生信息, {}, {}, {}", name, degree, clazzId);
        List<StudentModel> studentModelList = stdService.list(name, degree, clazzId);
        //response.setHeader("Content-Disposition", "attachment;filename=student.xlsx");
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        EasyExcel.write(response.getOutputStream(), StudentModel.class)
                .sheet("学员信息列表")
                .registerWriteHandler(new HorizontalCellStyleStrategy(null, writeCellStyle))
                .doWrite(studentModelList);
    }

}
